package com.ruoyi.project.ins.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ins.domain.InsPrimaryAccount;
import com.ruoyi.project.ins.service.IInsPrimaryAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ins/primaryAccount")
public class InsPrimaryAccountController extends BaseController {
    @Autowired
    private IInsPrimaryAccountService iInsPrimaryAccountService;

    @PreAuthorize("@ss.hasPermi('ins:primaryAccount:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsPrimaryAccount insPrimaryAccount)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insPrimaryAccount.setCreateBy(username);
        }
        startPage();
        List<InsPrimaryAccount> list = iInsPrimaryAccountService.selectInsPrimaryAccountList(insPrimaryAccount);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ins:primaryAccount:add')")
    @Log(title = "ins主号管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InsPrimaryAccount insPrimaryAccount)
    {
        insPrimaryAccount.setCreateBy(getUsername());
        insPrimaryAccount.setStatus("0");
        return toAjax(iInsPrimaryAccountService.insertInsPrimaryAccount(insPrimaryAccount));
    }

    @PreAuthorize("@ss.hasPermi('ins:primaryAccount:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(iInsPrimaryAccountService.selectInsPrimaryAccountById(id));
    }

    @PreAuthorize("@ss.hasPermi('ins:primaryAccount:edit')")
    @Log(title = "ins主号管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InsPrimaryAccount insPrimaryAccount)
    {
        insPrimaryAccount.setUpdateBy(getUsername());
        return toAjax(iInsPrimaryAccountService.updateInsPrimaryAccount(insPrimaryAccount));
    }
}
